package create_tenant

import (
	"context"
	constants2 "git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/repository/common"
	"gorm.io/gorm"
	"time"
)

type TenantsCreateTaskRepository struct {
	common.TenantServiceDB
}

func NewTenantsCreateTaskRepository() TenantsCreateTaskRepositoryIface {
	return &TenantsCreateTaskRepository{}
}

func (g *TenantsCreateTaskRepository) AddTenantsCreateTask(ctx context.Context, data *TenantsCreateTask) (int32, error) {
	data.CreatedOn = time.Now()
	data.ModifiedOn = time.Now()
	err := g.GetTenantServiceDB(ctx).Create(&data).Error
	if err != nil {
		return 0, err
	}
	return data.Id, nil
}

func (g *TenantsCreateTaskRepository) SaveTenantsCreateTask(ctx context.Context, data *TenantsCreateTask) error {
	data.ModifiedOn = time.Now()
	err := g.GetTenantServiceDB(ctx).Save(data).Error
	if err != nil {
		return err
	}
	return nil
}

func (g *TenantsCreateTaskRepository) GetTaskByTenantCode(ctx context.Context, tenantCode string) (*TenantsCreateTask, error) {
	tenantsCreateTask := &TenantsCreateTask{}
	err := g.GetTenantServiceDB(ctx).
		Table(TenantsCreateTask{}.TableName()).
		Where("is_deleted = ?", constants2.IsDeletedFalse).
		Where("tenant_code = ?", tenantCode).
		Order("created_on desc").First(tenantsCreateTask).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return tenantsCreateTask, err
	}
	return tenantsCreateTask, nil
}

func (g *TenantsCreateTaskRepository) GetTenantsCreateTask(ctx context.Context, taskId string) (*TenantsCreateTask, error) {
	tenantsCreateTask := &TenantsCreateTask{}
	err := g.GetTenantServiceDB(ctx).
		Table(TenantsCreateTask{}.TableName()).
		Where("is_deleted = ?", constants2.IsDeletedFalse).
		Where("task_id = ?", taskId).
		Order("created_on desc").First(tenantsCreateTask).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return tenantsCreateTask, err
	}
	return tenantsCreateTask, nil
}

// GetEffectiveTask 获取有效的任务：只有状态是”失败“，且重试次数超过5次，才是无效任务，其他都是有效任务。当不存在有效任务时，会新增任务
func (g *TenantsCreateTaskRepository) GetEffectiveTask(ctx context.Context, tenantCode string) (*TenantsCreateTask, error) {
	tenantsCreateTask := &TenantsCreateTask{}
	err := g.GetTenantServiceDB(ctx).
		Table(TenantsCreateTask{}.TableName()).
		Where("is_deleted = ?", constants2.IsDeletedFalse).
		Where("tenant_code = ?", tenantCode).
		Where("(status!='2' or (status='2' and retry_times<=5 ))").
		Order("created_on desc").First(tenantsCreateTask).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return tenantsCreateTask, err
	}
	return tenantsCreateTask, nil
}

func (g *TenantsCreateTaskRepository) UpdateStatus(ctx context.Context, tenantCode string, taskId string, status int32) error {
	err := g.GetTenantServiceDB(ctx).
		Table(TenantsCreateTask{}.TableName()).
		Where("tenant_code=?", tenantCode).
		Where("task_id=?", taskId).
		Where("is_deleted = ?", constants2.IsDeletedFalse).
		Updates(map[string]interface{}{"status": status}).Error
	if err != nil {
		return err
	}
	return nil
}
